package com.android.lovely.data

import com.android.lovely.room.BotCacheService
import com.android.lovely.room.BotCacheService.convertToUniqueId
import com.android.lovely.room.CacheProfile
import com.android.lovely.room.ProfileDatabase
import com.facebook.ProfileManager
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 *
 * @author why
 * @date 2025/3/11 16:54
 */
@JsonClass(generateAdapter = true)
data class ChatHistoryList(
    @Json(name = "ai_type") val aiType: Int = 0,
    @Json(name = "app_id") val appId: Int = 0,
    @Json(name = "app_name") val appName: String = "",
    @Json(name = "avatar") val avatar: Map<String, String> = emptyMap(),
    @Json(name = "avatar_detail") val avatarDetail: String = "",
    @Json(name = "background_img") val backgroundImg: String = "",
    @Json(name = "bot_id") val botId: Int = 0,
    @Json(name = "brainwash_template_id") val brainwashTemplateId: Int = 0,
    @Json(name = "c_time") val cTime: String = "",
    @Json(name = "card_num") val cardNum: Int = 0,
    @Json(name = "category") val category: Int = 0,
    @Json(name = "chat_dec") val chatDec: String = "",
    @Json(name = "chat_example") val chatExample: String = "",
    @Json(name = "chat_gender") val chatGender: Int = 0,
    @Json(name = "chat_model") val chatModel: Map<String, String>? = emptyMap(),
    @Json(name = "chat_name") val chatName: String = "",
    @Json(name = "chat_num") val chatNum: Int = 0,
    @Json(name = "click_num") val clickNum: Int = 0,
    @Json(name = "collect_num") val collectNum: Int = 0,
    @Json(name = "cover_image") val coverImage: Map<String, String> = emptyMap(),
    @Json(name = "description") val description: String = "",
    @Json(name = "draft_id") val draftId: Int = 0,
    @Json(name = "first_message") val firstMessage: String = "",
    @Json(name = "gender") val gender: Int = 0,
    @Json(name = "id") val id: Int = 0,
    @Json(name = "intimacy_value") val intimacyValue: Int = 0,
    @Json(name = "is_collect") val isCollect: Boolean = false,
    @Json(name = "is_translate") val isTranslate: Int = 0,
    @Json(name = "is_vip") val isVip: Int = 0,
    @Json(name = "last_chat_id") val lastChatId: Int = 0,
    @Json(name = "last_reply") val lastReply: String = "",
    @Json(name = "last_reply_time") val lastReplyTime: String = "",
    @Json(name = "last_reply_time_int") val lastReplyTimeInt: Int = 0,
    @Json(name = "min_chat_num") val minChatNum: Int = 0,
    @Json(name = "name") val name: String = "",
    @Json(name = "personality") val personality: String = "",
    @Json(name = "point") val point: Int = 0,
    @Json(name = "pro_point") val proPoint: Int = 0,
    @Json(name = "really_collect_num") val reallyCollectNum: Int = 0,
    @Json(name = "role") val role: String = "",
    @Json(name = "scenario") val scenario: String = "",
    @Json(name = "score") val score: Int = 0,
    @Json(name = "search_tag") val searchTag: String = "",
    @Json(name = "show_id") val showId: Int = 0,
    @Json(name = "sort") val sort: Int = 0,
    @Json(name = "star") val star: Int = 0,
    @Json(name = "status") val status: Int = 0,
    @Json(name = "tag_id_string") val tagIdString: String = "",
    @Json(name = "type") val type: Int = 0,
    @Json(name = "u_time") val uTime: String = "",
    @Json(name = "unread_num") val unreadNum: Int = 0,
    @Json(name = "user_id") val userId: Int = 0,
    @Json(name = "user_info") val userInfo: Map<String, String>? = emptyMap(),
    @Json(name = "tag_list") val tagList: List<Map<String, String>>? = emptyList()
) {
    private fun asProfile(): CacheProfile {

        return CacheProfile(
            id.convertToUniqueId(),
            avatar["url"].orEmpty(),
            id,
            intimacyValue,
            false,
            scenario,
            name,
            cardNum,
            personality,
            point,
            gender,
            if (isCollect) 1 else 0,
            backgroundImg,
            false,
            chatName,
            chatGender,
            chatDec,
            userInfo?.getOrDefault("name", "").orEmpty(),
            userInfo?.getOrDefault("id", "0")?.toIntOrNull() ?: 0,
            tagList?.joinToString { it["tag_name"].orEmpty() }.orEmpty()
        )
    }

    suspend fun insertOrUpdateProfile() {
        if (BotCacheService.doesBotExist(id).not()) {
            ProfileDatabase.dao.insert(asProfile())
        } else {
            BotCacheService.updateBot(this@ChatHistoryList.id) {
                avatar = this@ChatHistoryList.avatar["url"].orEmpty()
                intimacyValue = this@ChatHistoryList.intimacyValue
                scenario = this@ChatHistoryList.scenario
                name = this@ChatHistoryList.name
                cardNum = this@ChatHistoryList.cardNum
                personality = this@ChatHistoryList.personality
                point = this@ChatHistoryList.point
                gender = this@ChatHistoryList.gender
                isCollect = if (this@ChatHistoryList.isCollect) 1 else 0
                background = this@ChatHistoryList.backgroundImg
                chatName = this@ChatHistoryList.chatName
                chatGender = this@ChatHistoryList.chatGender
                chatDec = this@ChatHistoryList.chatDec
                hasChatMode = this@ChatHistoryList.chatModel.isNullOrEmpty().not()
                userName = userInfo?.getOrDefault("name", "").orEmpty()
                userId = userInfo?.getOrDefault("id", "0")?.toIntOrNull() ?: 0
                roleTag = tagList?.joinToString { it["tag_name"].orEmpty() }.orEmpty()
            }
        }
    }
}